'''
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License") you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
'''

'''
 * Datatype representation for xsd:float. Can't just use XSDBaseNumericType
 * because float, double and decimal are all disjoint in XSD. Can use plain
 * XSDDatatype because the equality function needs overriding.
'''

from base.datatypes.xsd.XSDDatatype import XSDDatatype
import sys

class XSDFloat(XSDDatatype):
    '''
     * Constructor.
     *
     * @param typeName the name of the XSD type to be instantiated, this is
     *                 used to lookup a type definition from the Xerces schema factory.
    '''
    def __init__(self, typeName):
        super().__init__(typeName)

    '''
     * Constructor.
     *
     * @param typeName  the name of the XSD type to be instantiated, this is
     *                  used to lookup a type definition from the Xerces schema factory.
     * @param javaClass the java class for which this xsd type is to be
     *                  treated as the cannonical representation
    '''
    # public XSDFloat(String typeName, Class<?> javaClass):
    #     super(typeName, javaClass)
    # 

    # # @Override
    # param valueFrom
    # return boolean
    def isValidValue(self, valueForm):
        return isinstance(valueForm, float)

    '''
     * Parse a lexical form of this datatype to a value
     *
     * @throws DatatypeFormatException if the lexical form is not legal
    '''
    # # @Override
    # param String
    # reutrn Object
    # throws DatatypeFormatException
    def parse(self, lexicalForm):
        self.checkWhitespace(lexicalForm)
        return super().parse(lexicalForm)

    # @Override
    # param Object
    # return String
    def unparse(self, value):
        if isinstance(value, float):
            # Java has "Infinity" and -"Infinity" but XSD has "INF" and "-INF"
            if value == sys.float_info.max:
                return 'INF'
            elif value == -1 * sys.float_info.max:
                return '-INF'
            return str(value)
        return super().unparse(value)
    

    '''
     * Parse a validated lexical form. Subclasses which use the default
     * parse implementation and are not converted by the explicit convertValidatedData
     * cases should override this.
    '''
    # # @Override
    # param lex
    # return Object
    def parseValidated(self, lex):
        if lex == 'INF':
            return sys.float_info.max
        elif lex == '-INF':
            return sys.float_info.max * -1
        elif lex == 'NaN':
            return float('nan')
        return float(lex)

    '''
     * Check for whitespace violations.
     * Turned off by default.
    '''
    # param String
    # return void
    # default to passed
    def checkWhitespace(self, lexicalForm):
        pass
        # if (JenaParameters.enableWhitespaceCheckingOfTypedLiterals):
        #     if not lexicalForm.trim().equals(lexicalForm):
        #         raise DatatypeFormatException(lexicalForm, this, "whitespace violation")
    '''
     * Compares two instances of values of the given datatype.
     * This ignores lang tags and just uses the java.lang.Number
     * equality.
    '''
    # @Override
    # param LiteralLabel LiteralLabel
    # return boolean
    def isEqual(self, value1, value2):
        return value1.getDatatype() == value2.getDatatype() \
                and value1.getValue().equals(value2.getValue())